package com.example.demo.response;


import com.fasterxml.jackson.annotation.JsonInclude;

import java.time.Instant;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiResponse<T> {
    private int code;
    private String status;
    private String message;
    private T data;
    private String timestamp;
    private String path;
    private String traceId;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getExtra() {
        return extra;
    }

    public void setExtra(Object extra) {
        this.extra = extra;
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    private Object extra;

    private ApiResponse(int code, String status, String message, T data, String path, String traceId, Object extra) {
        this.code = code;
        this.status = status;
        this.message = message;
        this.data = data;
        this.timestamp = Instant.now().toString();
        this.path = path;
        this.traceId = traceId;
        this.extra = extra;
    }

    public static <T> ApiResponse<T> success(T data, String message, String path, Object extra) {
        return new ApiResponse<>(200, "success", message, data, path, java.util.UUID.randomUUID().toString(), extra);
    }

    public static <T> ApiResponse<T> created(T data, String message, String path) {
        return new ApiResponse<>(201, "success", message, data, path, java.util.UUID.randomUUID().toString(), null);
    }

    public static <T> ApiResponse<T> fail(int code, String message, String path) {
        return new ApiResponse<>(code, "fail", message, null, path, java.util.UUID.randomUUID().toString(), null);
    }

    public static <T> ApiResponse<T> error(String message, String path) {
        return new ApiResponse<>(500, "error", message, null, path, java.util.UUID.randomUUID().toString(), null);
    }
}